﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Blazing.Web.Controls
{
    [Themeable(true)]
    public class BreadcrumbControl : UserControl
    {
        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            string totalFragment = "/";
            List<string> elements = new List<string>(Request.RawUrl.Split('/'));

            if (elements[elements.Count - 1].ToLower().Contains("default.aspx") ||
                elements[elements.Count - 1].ToLower().Contains("content.aspx"))
                elements.RemoveAt(elements.Count - 1);

            for (int i = 0; i < elements.Count;i++ )
            {
                string str = elements[i];
                if (str.Equals("/") || string.IsNullOrEmpty(str))
                    continue;

                if (totalFragment.Length > 1)
                {
                    LiteralControl breaker = new LiteralControl();
                    breaker.Text = " &gt; ";
                    this.Controls.Add(breaker);
                }

                totalFragment += str + "/";

                if (i == elements.Count - 1)
                {
                    LiteralControl current = new LiteralControl();
                    current.Text = str.Replace("-", " ").Replace(".aspx","");
                    this.Controls.Add(current);
                }
                else
                {
                    HyperLink link = new HyperLink();
                    link.NavigateUrl = totalFragment;
                    link.Text = str.Replace("-", " ");
                    Controls.Add(link);
                }
            }
        }
    }
}
